package com.xm.xmwa1706090.service.model;


import com.xm.xmwa1706090.service.util.CodeUtil;
import com.xm.xmwa1706090.service.util.HexCodeUtils;

/**
 * @Project : 硬件通讯
 *
 * @Description : {
字段	长度	备注                                                  示例
start   2        开始位                                                FA FA
length	2	    数据长度version+cmd+type+termed+seq+parm               00 00
version	2	    协议版本 主版本号在前 次版本号在后                     00 00
Cmd	    2	    操作命令                                               00 00
Type	2	    请求：1，回应:2                                        00 00
termID	12	    停车场ID号，字符串，由云平台统一分配                   00 00 00 00 00 00 00 00 00 00 00 00 00
Seq	    4 	    包序列号，用来唯一的标识一次请求和回应                 00 00 00 00
flag	4	    固定为0                                                00 00 00 00
Param	不定	报文正文，参数列表或结果回应, JSON 字符串
end     2       结束位 AFAF                                            AFAF
Crc16	2	    校验码前所有字节的 CRC 校验，X^16+X^15+X^2+1  00 00

请求和回应包的cmd相同；
请求的type为1，回应的包type为2；
请求和回应包的Seq相同；
param格式为JSON 字符串，JSON中的键值都是字符串型的，如缴费金额键值对为"amount": "100"，不能为"amount": 100

Crc16	2	校验码前所有字节的 CRC 校验，生成多项式：X^16+X^15+X^2+1 ，高位字节在前，低位字节在后。校验算法参考附录
}
 */
public class SocketEntity {

    String start ;
    String  length;
    String version;
    String cmd;
    String type ;
    String  termid;
    String  seq;
    String  flag;
    String  param;
    String end ;
    String crc16;

    public SocketEntity(){
        this.start = "FAFA";
        this.length="0034";
        this.cmd = "0001";
        this.type="0001" ;
        this.termid="000000000000000000000001" ;
        this.seq="0001" ;
        this.flag="00000101";
        this.end = "AFAF";
    }

    public SocketEntity(int cmd, int type, int termid, String seq, int flag, String param) {
        this.start = "FAFA";
        this.cmd = HexCodeUtils.tenToHexPadLeft( cmd,4);
        this.version = HexCodeUtils.tenToHexPadLeft( 1,2)+HexCodeUtils.tenToHexPadLeft( 0,2) ;
        this.type= HexCodeUtils.tenToHexPadLeft( type,4) ;
        this.termid=HexCodeUtils.tenToHexPadLeft( termid ,24);
        this.seq = seq ;
        this.flag = HexCodeUtils.tenToHexPadLeft( 0,8);
        this.param = param ;
        this.end = "AFAF";
        int lengths = (2+2+2+12+4 ) * 2  + param.getBytes().length ;
        this.length = HexCodeUtils.tenToHexPadLeft( lengths,4);

        byte[] crc16_data = CodeUtil.hexStringToBytes(
                this.start + this.length+this.version
                +this.cmd + this.type + this.termid + this.seq + this.flag + this.param + this.end
                );
        String crc16 = CodeUtil.CRC16Table(crc16_data);
        this.crc16  = crc16.substring(0, 4);

    }


    /**
     * @updateTime 6-21 15:45
     * @author loysen
     * @Description {
     *      对数据进行编码
     *   }
     * @return
     */
    public  byte[] EnCode(){
        String data = this.start+this.length+this.version+this.cmd+this.type+this.termid+this.seq+this.flag;
        String data1 = this.end+this.crc16;
        return CodeUtil.byteMerger( CodeUtil.byteMerger( CodeUtil.hexStringToBytes(data) , this.param.getBytes() )  ,  CodeUtil.hexStringToBytes(data1) );
    }

    /**
     * (non-Javadoc)
     * @see Object#toString()
     */
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "start:"+ this.start +"\n length :"+this.length+"\n version:"+this.version+"\n cmd:"+this.cmd+"\n type: "+this.type+"\n termid: "+this.termid+"\n  seq: "+this.seq+"\n flag: "+this.flag+"\n param: "+this.param +"\n end:"+this.end + "\n crc16:"+this.crc16 ;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTermid() {
        return termid;
    }

    public void setTermid(String termid) {
        this.termid = termid;
    }

    public String getSeq() {
        return seq;
    }

    public void setSeq(String seq) {
        this.seq = seq;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getCrc16() {
        return crc16;
    }

    public void setCrc16(String crc16) {
        this.crc16 = crc16;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
